<template>
  <div>
    <common-head title="修改联系人"/>
    <div class="wrap">
      <van-address-edit
        :address-info="addressInfo"
        :area-list="areaList"
        show-delete
        show-postal
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="deleteContact"
      />
    </div>
  </div>
</template>

<script>
import CommonHead from '../../components/CommonHead.vue'
import { areaList } from '@vant/area-data'
import { Toast } from 'vant'
import { editContact, getContact, deleteContact } from 'api'
import { getToken } from '../../utils/index'
export default {
  data () {
    return {
      areaList,
      addressInfo: {
        // 要修改的联系人的默认值
      }
    }
  },
  created () {
    this.getContact()
  },
  methods: {
    deleteContact () {
      deleteContact({
        id: this.$route.query.id,
        token: getToken()
      }).then(res => {
        console.log(res)
        Toast({
          type: 'success',
          message: '删除成功',
          onClose: () => {
            this.$router.push('/contactLists')
          }
        })
      })
    },
    getContact () {
      // 获取编辑联系人的初始值
      getContact({
        id: this.$route.query.id,
        token: getToken()
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          const obj = {}
          const contact = res.data.data.info
          for (const key in contact) {
            switch (key) {
              case 'id' :
                obj.id = contact[key]
                break
              case 'linkMan' :
                obj.name = contact[key]
                break
              case 'mobile' :
                obj.tel = contact[key]
                break
              case 'address' :
                obj.address = contact[key]
                break
              case 'isDefault' :
                obj.isDefault = contact[key]
                break
              case 'cityStr' :
                obj.county = contact[key]
                break
              default:
                break
            }
          }
          this.addressInfo = obj
        }
      })
    },
    // onSave里面有一个参数,参数就是当前输入的内容
    onSave (contact) {
      console.log(contact)
      const obj = {}
      for (const key in contact) {
        switch (key) {
          case 'addressDetail' :
            obj.address = contact[key]
            break
          case 'areaCode' :
            obj.cityId = contact[key]
            break
          case 'name' :
            obj.linkMan = contact[key]
            break
          case 'tel' :
            obj.mobile = contact[key]
            break
          case 'postalCode' :
            obj.provinceId = contact[key]
            break
          default:
            break
        }
      }
      obj.isDefault = contact.isDefault
      obj.id = this.$route.query.id
      console.log(obj)
      editContact({
        ...obj,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          Toast({
            type: 'success',
            message: '修改成功',
            onClose: () => {
              this.$router.push('/contactLists')
            }
          })
        }
      })
    }
  },
  components: {
    CommonHead
  }

}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top: 46px;
}
</style>
